package ywh.player.media
{
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	import flash.geom.Rectangle;
	import ywh.list.CourseItem;
	import ywh.manager.Core;
	import ywh.manager.Sync;
	import ywh.manager.Tag;
	import ywh.player.AbstractShower;
	import ywh.player.RelayoutSprite;
	import ywh.ui.PhotoTile;
	import ywh.utils.XMLUtils;
	
	/**
	 * 图片列表浏览器
	 * @author 杨伟华
	 */
	public class PicsListShower extends AbstractShower
	{
		private var _photoTile:PhotoTile;
		private var _startTimeName:String;
		private var _urlName:String;
		private var _path:String;
		
		public function PicsListShower()
		{
		 	_startTimeName = Tag._START_TIME.substring(1);
			_urlName = Tag._URL.substring(1);
		}
		
		override protected function initDisplayer():void
		{
			if (render)
			{
				_photoTile = render as PhotoTile;
			}
			else
			{
				render = _photoTile = new PhotoTile()
			}
			_photoTile.clickTime = XMLUtils.getPropNumber(_defalutConfig, "@clickTime");
			_photoTile.columnWidth = XMLUtils.getPropNumber(_defalutConfig, "@thumbsWidth");
			_photoTile.sourceFunction = getSource;
			_photoTile.addEventListener(ListEvent.ITEM_CLICK, onNodeClick);
		}
		
		override public function set dataProvider(value:XML):void
		{
			_path = (value.hasOwnProperty(Tag._PARH) ? value[Tag._PARH][0] : "") + "thumbs/";
			_photoTile.dataProvider = new DataProvider(value);
		}
		
		private function getSource(item:Object):String
		{
			return Core.getResolvePath(_path + item[_urlName]);
		}
		
		override public function set playItem(item:CourseItem):void
		{
			super.playItem = item;
			var nodeIndex:int = findIndexByStartTime(item.startTime);
			if (nodeIndex > -1)
			{
				_photoTile.selectedIndex = nodeIndex;
				_photoTile.scrollToSelected();
			}
		}
		
		protected function findIndexByStartTime(startTime:Number):int
		{
			var array:Array = _photoTile.dataProvider.toArray();
			for (var i in array)
			{
				if (array[i][_startTimeName] == startTime)
				{
					return i;
				}
			}
			return -1;
		}
		
		private function onNodeClick(listEvent:ListEvent):void
		{
			var item:Object = listEvent.item;
			var time:Number = item.hasOwnProperty(_startTimeName) ? Number(item[_startTimeName]) : NaN;
			if (!isNaN(time))
			{
				Sync.time = time;
			}
			//防止树状菜单的选中状态;
			listEvent.preventDefault();
		}
	}

}